import { promptBox, toast, print,cacheTools } from 'nc-lightapp-front';
import {getQueryCondition} from '../../../../hrpub/common/utils/utils'


let staticUrl = {
    printCretUrl: '/nccloud/hrtrn/dimissionmng/DimissionHandlePrintCertAction.do', //打印证明
    printDisListUrl: '/nccloud/hrtrn/dimissionmng/DimissionHandlePrintListAction.do' //打印离职交接单
}

export default class BtnAction {
    constructor (comp) {
        this.comp = comp
    }

    onButtonClick = async(props, btn) => {
        const { action } = this.comp
        const { main, dispatch, search, modal } = props
        let number = props.editTable.getNumberOfRows('list')
        let billid = ''
        if(number > 0 && main.showPage === 'list'){
           billid = props.editTable.getValByKeyAndIndex('list',main.focusIndex,'pk_hi_stapply').value
        }else if( main.showPage === 'card'){
            billid = props.form.getFormItemsValue('card','pk_hi_stapply').value
        }
        switch (btn) {
            case 'query':
                search.openAdvSearch('query', true)
                break;
            case 'confirm':
                // let _this = this
                let content = main.json['gx6008-000196']
                if(number === 0){
                    toast({color: 'warning',content: main.json['gx6008-000285']})
                    return
                }
                if(main.orderStatus !== '3'){
                    content = main.json['gx6008-000197']
                }
                promptBox(
                    {
                        color: "warning",
                        title: main.json['i6013-000050'], /* 国际化处理： 提示*/
                        content: content, /* 国际化处理： */
                        beSureBtnClick: async ()=> {
                            let date;
                            if(main.showPage === 'list'){
                                date = props.editTable.getValByKeyAndIndex('list',main.focusIndex,'effectdate').value;
                            }else{
                                date = props.form.getFormItemsValue('card','effectdate').value;
                            }
                            await dispatch({
                                type: 'main/update',
                                payload: {
                                    dateData: date
                                }
                            })
                            modal.show('dimissionModal');
                        }
                    }
                )
                break;
            case 'printjjd':
                let postData = {
                    appcode: '60092080',
                    nodekey: '60081070jjd',
                    oids: [billid]
                };
                print('pdf', staticUrl.printDisListUrl, postData);
                break;
            case 'printzm':
                let param = {
                    appcode: '60092080',
                    nodekey: '60081070zm',
                    oids: [billid]
                };
                print('pdf', staticUrl.printCretUrl, param);
                break;
            case 'suspend': 
                    try {
                        let res = await dispatch({
                            type: 'main/suspend',
                            payload: {
                                billids: [billid],
                                pk_org: main.orgvalue.refpk
                            }
                        });
                        if (res.success) {
                            if (this.checkErrorMsg(res)) {
                                toast({
                                    color: 'success',
                                    content: main.json['gx6008-000047'] // 提交成功
                                });
                            }
                        }
                    } catch (e) {
                    }
                    if (main.showPage === 'list') {
                        this.pubSub.publish('queryTableData',{})
                    }
                break;
            default:
                break;
        }
    }

    // 检查返回值里是否有 errorMsg
    checkErrorMsg = (res) => {
        if (res && res.data && (res.data.errorMsg || res.data.message)) {
            toast({
                color: 'danger',
                content: res.data.errorMsg || res.data.message
            });
            return false
        }

        return true;
    }
    //查询模版查询事件
    searchBtn = () => {
        const { props } = this.comp
        const { search } = props
        this.pubSub.publish('queryTableData', {
            conditions: getQueryCondition(props,'query')
        })
        search.openAdvSearch('query', false) //关闭查询模版
    }
}